package tech.anzhen.simple.rpc.support.rest;

import java.io.PrintWriter;
import java.io.StringWriter;

public class ResponseMessageBuilder {

    public static ResponseMessageBuilder custom() {
        return new ResponseMessageBuilder();
    }

    private boolean success = true;

    private String code;

    private String message;

    private Object data;

    private String stack;

    private String moduleCode;

    private String moduleErrorCode;

    public ResponseMessageBuilder success() {
        this.success = true;
        return this;
    }

    public ResponseMessageBuilder fail() {
        this.success = false;
        return this;
    }

    public ResponseMessageBuilder code(String code) {
        this.code = code;
        return this;
    }

    public ResponseMessageBuilder message(String message) {
        this.message = message;
        return this;
    }

    public ResponseMessageBuilder data(Object data) {
        this.data = data;
        return this;
    }

    public ResponseMessageBuilder stack(String stack) {
        this.stack = stack;
        return this;
    }

    public ResponseMessageBuilder moduleCode(String moduleCode) {
        this.moduleCode = moduleCode;
        return this;
    }

    public ResponseMessageBuilder moduleErrorCode(String moduleErrorCode) {
        this.moduleErrorCode = moduleErrorCode;
        return this;
    }

    public ResponseMessageBuilder stack(Throwable throwable) {
        StringWriter writer = new StringWriter();
        throwable.printStackTrace(new PrintWriter(writer));
        this.stack = writer.toString();
        return this;
    }

    public ResponseMessage build() {
        return new ResponseMessage(success, message, code, stack, data, moduleCode, moduleErrorCode);
    }
}
